/**
 * @author Hepan
 * @description 公共函数定义
 */
interface JSApiCallProps {
  appId: string,
  nonceStr: string,
  package: string,
  signType: string,
  timeStamp: string,
  paySign: string
}
export default {
  //获取浏览器地址栏参数值
  getUrlParam(name: any){
    let reg = new RegExp('(^|&)'+name+'=([^&]*)');
    let r = window.location.search.substr(1).match(reg);
    if(r!=null)return decodeURIComponent(r[2]);
  },
  initShareInfo(wx: any){
    let shareInfo = {
      title: '支付分享', // 分享标题
      desc: '支付分享专项', // 分享描述
      link: 'http://m.51purse.com/#/index', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
      imgUrl: '', // 分享图标
    }
    wx.onMenuShareAppMessage(shareInfo);
    wx.onMenuShareTimeline(shareInfo);
    wx.onMenuShareQQ(shareInfo);
    wx.onMenuShareQZone(shareInfo);
    // wx.updateAppMessageShareData(shareInfo);
    // wx.updateTimelineShareData(shareInfo);
  },
  jsApiCall(payJson: JSApiCallProps, WeixinJSBridge: any) {
    console.log('payJson:', payJson)
    return new Promise((resolve, reject) => {
      WeixinJSBridge.invoke(
        'getBrandWCPayRequest', {
          appId: payJson.appId,
          nonceStr: payJson.nonceStr,
          package: payJson.package,
          signType: payJson.signType,
          timeStamp: payJson.timeStamp,
          paySign: payJson.paySign,
        }, (res: any) => {
          // 下单成功后，要基于当前后端返回值去判断resolve reject
          console.log('支付回调页面:', res)
          resolve(res)
        }
      )
    })
    // console.log("payJson:", payJson)
    // WeixinJSBridge.invoke(
    //   'getBrandWCPayRequest', {
    //   appId: payJson.appId,
    //   nonceStr: payJson.nonceStr,
    //   package: payJson.package,
    //   signType: payJson.signType,
    //   timeStamp: payJson.timeStamp,
    //   paySign: payJson.paySign,
    // }, (res: any) => {
    //     console.log('支付页面：', res)
    //     if (res.err_msg === 'get_brand_wcpay_request:ok') {
    //       // eslint-disable-next-line no-undef
    //       WeixinJSBridge.log(res.err_msg);
    //       // if (orderType === 1) {
    //       //   window.location.href = `/cicp/platform/buy/regimentType?orderSn=${orderInfo.regiment_sn}&orderType=${orderType}`
    //       // } else if (orderType === 2) {
    //       //   window.location.href = `/cicp/platform/buy/regimentType?goodId=${goodId}&orderType=${orderType}`
    //       // }
    //     } else {
    //       // this.Message.show({
    //       //   msg: '支付失败',
    //       //   clickClose: true, // 点击蒙层可关闭
    //       //   btn: ['确认'],
    //       //   onConfirm: () => {
    //       //     // window.history.go(-1)
    //       //   },
    //       // })
    //     }
    //   },
    // )
  }
}